package org.aiforum.backend.service;

import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;
import javax.transaction.Transactional.TxType;

import org.aiforum.backend.common.QueryPreperty;
import org.aiforum.backend.common.Response;
import org.aiforum.backend.mapper.AuthMapper;
import org.aiforum.domain.Auth;
import org.aiforum.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("authService")
@Transactional(TxType.REQUIRED)
public class AuthServiceImpl implements AuthService {
    
	@Autowired
	private AuthMapper authMapper;
	
	@Override
	public Response insert(Auth auth) {
		//初始化权限对象
		auth.setState(1);
		auth.setCreateDate(new Date());
		authMapper.insert(auth);
		Response response=Response.getInstaner();
		response.setState(true);
		response.setMessage("Add success");
		return response;
	}

	@Override
	public Response update(Auth auth) {
		//初始化权限对象
		auth.setState(1);
		authMapper.update(auth);
		Response response=Response.getInstaner();
		response.setState(true);
		response.setMessage("update success");
		return response;
	}

	@Override
	public Response findById(int authId) {
		Auth auth=authMapper.find(authId);
		Response response=Response.getInstaner();
		response.setState(true);
		response.setData(auth);
		return response;
	}

	@Override
	public Response delete(int authId) {
		authMapper.delete(authId);
		Response response=Response.getInstaner();
		response.setState(true);
		response.setMessage("Delete success");
		return response;
	}

	@Override
	public List<Auth> querys(QueryPreperty queryPreperty) {
		//添加判断获取到的搜索语句是否为空
		if(StringUtil.isNotEmpty(queryPreperty.getQueryValue().toString())){
			//构建模糊查询语句
			String value="%"+queryPreperty.getQueryValue()+"%";
		    queryPreperty.setQueryValue(value);
		}else{
			queryPreperty.setQueryValue(null);
		}
		return authMapper.querys(queryPreperty);
	}

	@Override
	public Auth find(int authId) {
		Auth auth=authMapper.find(authId);
		return auth;
	}

	@Override
	public Response checkExists(QueryPreperty queryPreperty) {
		//根据属性查询是否有记录
		int count=authMapper.checkExists(queryPreperty);
		Response response=Response.getInstaner();
		if(count>0){
			response.setState(true);
			response.setMessage(queryPreperty.getQueryName()+"already exists");
		}
		return response;
	}

	@Override
	public List<Auth> queryByIds(List<Integer> authIds) {
		Integer[] tempIds = authIds.toArray(new Integer[authIds.size()]);
		return authMapper.queryByIds(tempIds);
	}

}
